<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="roleList">
              <el-table-column
                align="center"
                type="index"
                label="序号"
                width="120"
              />
              <el-table-column
                align="center"
                prop="name"
                label="名称"
                width="240"
              />
              <el-table-column align="center" prop="description" label="描述" />
              <el-table-column align="center" label="操作" width="240">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hAssign(scope.row)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                layout="prev, pager, next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog :title="cTitle" :visible.sync="showDialog">
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="hCancel">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmitRole">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>

    <el-dialog title="分配权限" :visible.sync="showAssignDialog" @close="closeAssign" @open="openAssignDialog">
      <!-- 权限点数据展示 -->
      <el-tree
        ref="tree"
        :data="permissionData"
        :props="{ label: 'name' }"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="hCancelAssign">取消</el-button>
          <el-button type="primary" @click="hSubmitPermission">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole, updateRole, assignPerm, getRoleDetail } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { arryToTree } from '@/utils/index'
export default {
  data() {
    return {
      isEdit: false, // 是否是编辑
      showDialog: false,
      showAssignDialog: false, // 分配权限对话框
      roleId: null,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      permissionData: [], // 存储权限数据
      roleList: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0 // 记录总数
      }
    }
  },
  computed: {
    cTitle() {
      return this.isEdit ? '编辑角色' : '新增角色'
    }
  },
  mounted() {
    this.loadRoleList() // 获取角色列表
  },
  methods: {
    async hSubmitPermission() {
      await assignPerm({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配成功')
      this.showAssignDialog = false
    },
    openAssignDialog() {
      this.loadPermissionList()
    },
    closeAssign() {
      this.$refs.tree.setCheckedKeys([])
    },
    async loadPermissionList() {
      // 发送请求, 获取权限列表
      const data = await getPermissionList()
      console.log('权限列表的数据是', data)
      this.permissionData = arryToTree(data)

      // 发送请求, 获取当前默认权限id
      const roleDetail = await getRoleDetail(this.roleId)
      this.$refs.tree.setCheckedKeys(roleDetail.permIds)
    },
    hCancelAssign() {
      this.showAssignDialog = false
    },
    // 用户点击了权限分配按钮
    hAssign({ id }) {
      // 显示弹层
      this.showAssignDialog = true
      // 记下来id
      this.roleId = id
    },
    hCancel() {
      // 清空表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      // 关闭弹框
      this.showDialog = false
    },
    hEdit({ id, name, description }) {
      this.isEdit = true
      this.roleForm = { id, name, description }
      this.showDialog = true
    },
    hAdd() {
      this.isEdit = false
      this.showDialog = true
    },
    doAdd() {
      addRole(this.roleForm).then(res => {
        // 重新拉取数据
        this.loadRoleList()
        this.$message.success('操作成功')
        this.hCancel()
      })
    },
    doEdit() {
      updateRole(this.roleForm).then(res => {
        // 重新拉取数据
        this.loadRoleList()
        this.$message.success('操作成功')
        this.hCancel()
      })
    },
    hSubmitRole() {
      this.$refs.roleForm.validate(async valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    hDel(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除接口
        deleteRole(id).then(res => {
          this.loadRoleList()
          this.$message.success('删除角色成功')
        })
        // 提示用户删除成功
      }).catch()
    },
    async loadRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.roleList = rows
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.loadRoleList()
    }
  }
}
</script>
